package dev

import (
	"fmt"
	"time"

	"jgt-wallet/src/bll"
	"jgt-wallet/src/context"
	"jgt-wallet/src/schema"

	"github.com/spf13/viper"
)

type UnionpayB2b struct {
	UnionpayB2bBll *bll.UnionpayB2B `inject:""`
	OrderBll       *bll.Order       `inject:""`
	orderId        string
}

// 支付宝应用支付接口
func (a *UnionpayB2b) Pay(ctx *context.Context) error {
	var (
		key    string
		param  = new(schema.UnionpayB2BParams)
		err    error
		result string
	)
	a.orderId = fmt.Sprintf("%d", time.Now().Unix())
	param.OrderID = a.orderId
	param.Price = ctx.ParamInt64("price")
	param.Describe = fmt.Sprintf("测试请求[%d]", time.Now().Unix())
	param.Tag = "测试"

	key = "test"
	param.Url = fmt.Sprintf("%s/api/v1/public/alipay/callback/app/%s", viper.GetString("web_addr"), key)

	result, err = a.UnionpayB2bBll.Pay(key, param)
	if err != nil {
		return ctx.ResErrBadRequest(err)
	}

	return ctx.ResHtml([]byte(result))
}
